/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PackageDB;

import PackageAssgnment.Users;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.DatatypeConverter;

/**
 *
 * @author dungnv
 */
public class CodeUpdateDB {
    
    public String convertMD5(String password) throws NoSuchAlgorithmException{
          MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(password.getBytes());
        byte byteData[] = md.digest();
        //convert the byte to hex format method 1
//        StringBuffer sb = new StringBuffer();
//        for (int i = 0; i < byteData.length; i++) {
//         sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
//        }
        String hex = DatatypeConverter.printHexBinary(byteData);
        System.out.println("Digest(in hex format):: " + hex);
        return hex;
}
    
    
    
    public boolean Logins(Users u){
      
        Connection conn = null;
        try {
            conn = ConnectDB.getConnection();
            CallableStatement cstmt = conn.prepareCall("{call login_Users(?,?)}");
            cstmt.setString("UserName",u.getUserName());
            cstmt.setString("Password", convertMD5(u.getPassword()));
            ResultSet rs = cstmt.executeQuery();
            while(rs.next()){
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(CodeUpdateDB.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException ex) {
                    Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return false;
    }
}
